package leeon.mobile.server.bbscache.entity;

import java.util.Arrays;
import java.util.Date;
import java.util.List;

import javax.jdo.annotations.Extension;
import javax.jdo.annotations.PersistenceCapable;
import javax.jdo.annotations.Persistent;
import javax.jdo.annotations.PrimaryKey;

import leeon.mobile.server.bbscache.BBSUploadService;

import com.google.appengine.api.datastore.Text;

@PersistenceCapable
public class Doc {
	
	@PrimaryKey
	@Persistent
	private Long key = null;
	
	@Persistent
	private Long keyG = null;
	
	@Persistent
	@Extension(vendorName="datanucleus", key="gae.unindexed", value="true")
	private String author = null;
	
	@Persistent
	@Extension(vendorName="datanucleus", key="gae.unindexed", value="true")
	private Date date = null;
	
	@Persistent
	@Extension(vendorName="datanucleus", key="gae.unindexed", value="true")
	private String title = null;
	
	@Persistent
	private Text content = null;
	
	@Persistent
	@Extension(vendorName="datanucleus", key="gae.unindexed", value="true")
	private Integer contentLength = null;
		
	@Persistent
	@Extension(vendorName="datanucleus", key="gae.unindexed", value="true")
	private String board = null;
	
	
	public Doc(Long fid, Long gid, String author, Date date, String title, String board, Long bid) { 
		this.key = createKey(bid, fid);
		this.keyG = createKeyG(bid, gid, fid.equals(gid));
		this.author = author;
		this.date = date;
		this.title = title;
		this.board = board;
	}
	
	public Long getKey() {
		return key;
	}

	public void setKey(Long key) {
		this.key = key;
	}
	
	public Long getKeyG() {
		return keyG;
	}

	public void setKeyG(Long keyG) {
		this.keyG = keyG;
	}
	
	public Long getBid() {
		return parseBid(key);
	}

	public Long getFid() {
		return parseFid(key);
	}

	public Long getGid() {
		return parseGid(keyG);
	}
	
	public boolean isGdoc() {
		return parseGdoc(keyG);
	}
	
	public String getAuthor() {
		return author;
	}

	public void setAuthor(String author) {
		this.author = author;
	}

	public Date getDate() {
		return date;
	}

	public void setDate(Date date) {
		this.date = date;
	}

	public String getTitle() {
		return title;
	}

	public void setTitle(String title) {
		this.title = title;
	}

	public Text getContent() {
		return content;
	}

	public void setContent(Text content) {
		this.content = content;
	}
	
	public void setContentOldAndNew(String oldContent, String newContent) {
		this.contentLength = oldContent.length();
		this.content = new Text(oldContent + newContent);
	}
	
	public String getNewContent() {
		String ret;
		if (this.contentLength == null) return getOldContent();
		else ret = this.content.getValue().substring(this.contentLength);
		
		return ret.replace(BBSUploadService.PIC_URL_REAL, BBSUploadService.PIC_URL);
	}
	
	public String getOldContent() {
		String ret;
		if (this.contentLength == null) ret = this.content.getValue();
		else ret = this.content.getValue().substring(0, this.contentLength);
		
		return ret.replace(BBSUploadService.PIC_URL_REAL, BBSUploadService.PIC_URL);
	}

	public int getContentLength() {
		return contentLength;
	}

	public void setContentLength(int contentLength) {
		this.contentLength = contentLength;
	}

	public String getBoard() {
		return board;
	}

	public void setBoard(String board) {
		this.board = board;
	}
	
	public static final String[] ID_PREFIX = {
		"00000000000000",
		"0000000000000",
		"000000000000", 
		"00000000000",
		"0000000000",
		"000000000",
		"00000000",
		"0000000",
		"000000",
		"00000",
		"0000",
		"000",
		"00",
		"0",
		""
		};
	
	public static Long createKey(Long bid, Long fid) {
		return Long.valueOf(
				createBoardWithPrefix(bid) + 
				createIdWithPrefix(String.valueOf(fid)));
	}
	
	public static Long parseBid(Long key) {
		return Long.valueOf(String.valueOf(key).substring(0, 5))^0xFFFF;
	}
	
	public static Long parseFid(Long key) {
		return Long.valueOf(String.valueOf(key).substring(5));
	}
	
	public static Long createKeyG(Long bid, Long gid, boolean gdoc) {
		return 	Long.valueOf(
				createBoardWithPrefix(bid) +
				(gdoc?"1":"0") +
				createGIdWithPrefix(String.valueOf(gid)));
	}
	
	public static Long parseGid(Long keyG) {
		return Long.valueOf(String.valueOf(keyG).substring(6));
	}
	
	public static boolean parseGdoc(Long keyG) {
		return "1".equals(String.valueOf(keyG).substring(5, 6));
	}
	
	public static Long createBoardKey(Long bid) {
		return 	Long.valueOf(
				createBoardWithPrefix(bid) +
				createIdWithPrefix(""));
	}
	
	public static Long createBoardKeyG(Long bid) {
		return 	Long.valueOf(
				createBoardWithPrefix(bid) +
				"1" +
				createGIdWithPrefix(""));
	}
	
	public static Object[] creatBoardFilter(Long bid, Long startKey) {
		return Arrays.asList(
			startKey!=null?startKey:createBoardKey(bid), createBoardKey(bid-1))
			.toArray();
	}
	
	public static Object[] creatBoardFilter(Long bid) {
		return creatBoardFilter(bid, null);
	}
	
	public static Object[] creatBoardFilterG(Long bid) {
		return Arrays.asList(
			createBoardKeyG(bid), createBoardKey(bid-1))
			.toArray();
	}
	
	public static List<Long> createGdocFilter(Long bid, Long gid) {
		return Arrays.asList(
			createKeyG(bid, gid, true), createKeyG(bid, gid, false));
	}
	
	public static String createIdWithPrefix(String id) {
		return ID_PREFIX[id.length()]+id;
	}
	
	public static String createGIdWithPrefix(String id) {
		return ID_PREFIX[id.length()+1]+id;
	}
	
	public static String createBoardWithPrefix(Long bid) {
		return String.valueOf(bid^0xFFFF);
	}
	
	
	
//	public static void main(String[] s) {
//		System.out.println(createKey(544L, 3085182644L));
//		System.out.println(createKey(13L, 3085182644L));
//		System.out.println(createKeyG(13L, 3085182644L, true));
//		System.out.println(createKey(1544L, 9085182644L));
//		System.out.println(createKey(44L, 6085182644L));
//		System.out.println(createBoardKey(13L));
//		System.out.println(createBoardKeyG(13L));
//		
//		System.out.println(parseBid(createKey(544L, 3085182644L)));
//		System.out.println(parseFid(createKey(544L, 3085182644L)));
//		System.out.println(parseBid(createKey(13L, 3085182644L)));
//		System.out.println(parseGid(createKeyG(13L, 3085182644L, true)));
//		System.out.println(parseGdoc(createKeyG(13L, 3085182644L, true)));
//		System.out.println(parseGdoc(createKeyG(13L, 3085182644L, false)));
//	}
}
